<?php
/**
 * 库存管理
 */
class inventoryAction extends apiAction {
	
	/**
	 * 库存查看
	 */
	public function index(){
		$storeId = (int)I('storeId', $this->user['storeId']);		//营业厅id
		$store = D('store')->field("id,name")->where(array('id'=>$storeId))->find();
		$list = D('inventory')->field("i.brandId, b.name AS brandName, i.modelId, m.name AS modelName, i.salesInventory AS num")->alias('i')
				->join(C('DB_PREFIX')."model AS m ON m.id = i.modelId")
				->join(C('DB_PREFIX')."brand AS b ON b.id = m.brandId")
				->where(array('storeId'=>$storeId))->select();
		
		$data['inventory'] = $list;
		$data['store'] = array('storeId'=>$store['id'], 'storeName'=>$store['name']);
		
		$this->ajaxReturn(200, L('success'), $data);

	}
	
	/**
	 * 大区库存
	 * 
	 */
	public function all(){
		$where  = array('salesInventory'=> array('gt','0'));
		$list  = D(MODULE_NAME)->seachInventory($where);
		$this->assign('list', $list);
		$this->display();
	}
	
	/**
	 * 地市库存
	 * 
	 */
	public function city(){
		$cityId = I('cityId', $this->user['cityId'][0]);
		
		//@todo
		if(! $cityId){
			$microArea = D('region')->where(array("id"=>$this->user['storeRegionId']))->field("pid")->find();
			$cityId = $microArea['pid'];
		}
		
		$region = D('region')->where(array("id"=>$cityId))->field("name")->find();
		$where  = array('salesInventory'=> array('gt','0'), 'c.id'=>$cityId);
		$list  = D(MODULE_NAME)->seachInventory($where);
		$this->assign('name', $region['name']);
		$this->assign('list', $list);
		$this->display();
	}
	/**
	 * 库存查询
	 */
	public function search(){
		$storeId = (int)I('storeId');	//营业厅id
		$microAreaId = (int)I('microAreaId');	//片区id
		$cityId = (int)I('cityId');	//城市id
		$areaId = (int)I('areaId');	//大区id
		
		if($storeId){
			$region = D('store')->field("id,name")->where(array('id'=>$storeId))->find();
			$where  = array('salesInventory'=> array('gt','0'), 'storeId'=>$storeId);
		}elseif ($microAreaId) {
			$region = D('region')->where(array("id"=>$microAreaId))->field("name")->find();
			$where  = array('salesInventory'=> array('gt','0'), 'regionId'=>$microAreaId);
		}elseif ($cityId) {
			$region = D('region')->where(array("id"=>$cityId))->field("name")->find();
			$where  = array('salesInventory'=> array('gt','0'), 'c.id'=>$cityId);
		}elseif ($areaId) {
			$region = D('region')->where(array("id"=>$areaId))->field("name")->find();
			$where  = array('salesInventory'=> array('gt','0'), 'a.id'=>$areaId);
		}
		
		
		$list  = D(MODULE_NAME)->seachInventory($where);
		$this->assign('name', $region['name']);
		$this->assign('list', $list);
		$this->display();
	}
	
	/**
	 * 库存录入、更新
	 */
	public function set(){
		
		$storeId	= I('storeId');		//营业厅id
		$regionId	= I('microAreaId');	//微区域id
		$num		= I('num');			//库存数目
		$brandId	= I('brandId');		//品牌id
		$modelId	= I('modelId');		//机型id

		if(empty($num) || empty($brandId) || empty($modelId)){
			$this->ajaxReturn(300, L('error'));
		}
		
		foreach ($num as $key=>$val){
			$data = array();
			
			$data['storeId']	= $storeId;
			$data['regionId']	= $regionId;
			
			$data['brandId']	= $brandId[$key];
			$data['modelId']	= $modelId[$key];
			
//			if(in_array($this->user['roleId'], array(6, 7))){
//				//如果角色 id 为 6，7 则为促销员录入库存
//				$data['salesId'] = $this->user['id'];
//				$data['salesInventory'] = $val;
//				$data['salesUpdateTime'] = time();
//			}else{
//				//客户录入初始库存
//				$data['customerId'] = $this->user['id'];
//				$data['customerInventory'] = $val;
//				$data['customerUpdateTime'] = time();
//			}
			
			$data['salesId'] = $this->user['id'];
			$data['salesInventory'] = $val;
			$data['salesUpdateTime'] = time();
		
			if(D('Inventory')->where(array('modelId'=>$data['modelId'], 'storeId'=>$data['storeId']))->find()){
				D('Inventory')->where(array('modelId'=>$data['modelId'], 'storeId'=>$data['storeId']))->save($data);
			}else{
				D('Inventory')->add($data);
			}
		}

		$this->ajaxReturn(200, L('success'));
	}


}

/* End of file inventoryAction.class.php */